/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.pacs.model;

import com.cxhd.pacs.DataConfig;
import com.cxhd.pacs.tools.DateUtil;
import com.cxhd.pacs.tools.MainTool;
import com.cxhd.pacs.tools.PinYinUtil;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import java.util.Map;

/**
 *
 * @author Administrator
 */
public class HostInfo extends Model<HostInfo> {

    public String getHostId(Controller ctrl) {
        boolean flag = true;
        String hostId = getHId(ctrl.getPara("name"), ctrl.getPara("card_no"), ctrl.getPara("age"), ctrl.getPara("birth"), ctrl.getPara("phone"), ctrl.getPara("info_id"), ctrl.getPara("hospital_code"));
        // System.out.println(ctrl.getPara("card_no") + "00000000000000000");
//        if (ctrl.getPara("card_no") != null && !ctrl.getPara("card_no").equalsIgnoreCase("")) {
//            hostId = getByCardNo(ctrl.getPara("card_no"));
//            flag = hostId != null;
//        }
//        if (flag && hostId == null & ctrl.getPara("name") != null && ctrl.getPara("age") != null) {
//            hostId = getByNameAndSexAndAge(ctrl.getPara("name"), ctrl.getPara("sex"), ctrl.getPara("age"));
//        }
//        if (flag && hostId == null & ctrl.getPara("name") != null && ctrl.getPara("birth") != null) {
//            hostId = getByNameAndBirth(ctrl.getPara("name"), ctrl.getPara("birth"));
//        }
//        if (flag && hostId == null & ctrl.getPara("name") != null && ctrl.getPara("phone") != null) {
//            hostId = getByNameAndPhone(ctrl.getPara("name"), ctrl.getPara("phone"));
//        }
        if (hostId == null) {
            try {
                hostId = saveInfo(ctrl);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return hostId;

    }

    public synchronized String getHostId(Map<String, String> ctrl) {

        String hostId = ctrl.get("host_id");

        if (hostId == null) {

            hostId = getHId(ctrl.get("name"), ctrl.get("card_no"), ctrl.get("age"), ctrl.get("birth"), ctrl.get("phone"), ctrl.get("info_id"), ctrl.get("hospital_code"));

            if (hostId == null) {
                try {
                    hostId = saveInfo(ctrl);
                } catch (Exception e) {

                    e.printStackTrace();
                    return null;
                }
            } else if (ctrl.get("update_id") != null && ctrl.get("update_id").length() > 0) {
                updatePatient(ctrl);
            }
        } else if (getByHostId(hostId) == null) {
            try {
                saveInfo(ctrl);
            } catch (Exception e) {
                return null;
            }
        }

        return hostId;

    }

    public String getHId(String name, String card_no, String age, String birth, String phone, String info_id, String hospital_code) {

        String hostId = null;
        if (card_no != null && !card_no.equalsIgnoreCase("")) {
            hostId = getByCardNo(card_no);

        }

        if (hostId == null & name != null && phone != null && !phone.equalsIgnoreCase("")) {
            hostId = getByNameAndPhone(name, phone);
        }

        if (hostId == null & info_id != null && hospital_code != null && !hospital_code.equalsIgnoreCase("")) {
            System.out.println(hospital_code + "-----" + info_id);
            hostId = getByInfoIdAndHospitalCode(info_id, hospital_code);
            System.out.println(hostId + "#############");
        }
        return hostId;

    }

    public String getByCardNo(String card_no) {

        try {
            System.out.println("select * from hostinfo where card_no ='"+card_no+"'");
            return findFirst("select * from hostinfo where card_no ='"+card_no+"'").get("host_id");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public String getByNameAndBirth(String name, String birth) {
        try {
            return findFirst("select * from hostinfo where name =? and year(birth)=?", name, birth.split("-")[0]).get("host_id");
        } catch (Exception e) {
            //e.printStackTrace();
            return null;
        }
    }

    public String getByNameAndPhone(String name, String phone) {
        try {
            return findFirst("select * from hostinfo where name =? and phone=?", name, phone).get("host_id");
        } catch (Exception e) {
            // e.printStackTrace();
            return null;
        }
    }

    public String getByNameAndSexAndAge(String name, String sex, String age) {
        try {
            return findFirst("select * from hostinfo where name =? and sex=? and age=?", name, sex, age).get("host_id");
        } catch (Exception e) {
            //e.printStackTrace();
            return null;
        }
    }

    public String getByInfoIdAndHospitalCode(String info_id, String hospital_code) {
        try {
            if (hospital_code != null && info_id != null) {
                return Db.findFirst("select * from host_detail where info_id =? and hospital_code=? ", info_id, hospital_code).get("host_id");
            } else {
                return null;
            }
        } catch (Exception e) {
            //e.printStackTrace();
            return null;
        }

    }

    public String getByHostId(String hostId) {
        try {
            return findFirst("select * from hostinfo where host_id =? ", hostId).get("host_id");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public String saveInfo(Controller ctrl) {
        try {
            Map<String, String[]> map = ctrl.getParaMap();
            String hostId = MainTool.getInstance().getHostId();

            for (String key : map.keySet()) {
                if (!key.equals("uuid")) {
                    set(key, ctrl.getPara(key));
                }
            }
            set("pym", PinYinUtil.getPinYin(ctrl.getPara("name")));
            set("host_id", hostId).save();
            return hostId;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public void updatePatient(Map<String, String> ctrl) {
        ctrl.put("pym", PinYinUtil.getPinYin(ctrl.get("name")));
        if (ctrl.get("age") == null && ctrl.get("birth") != null) {
            ctrl.put("age", "" + DateUtil.getCurrentAgeByBirthdate(ctrl.get("birth")));
        }
        String sql = "update hostinfo set name=?,age=?,birth=?,address=?,phone=?,pym=? where info_id=?  ";
        Db.update(sql, ctrl.get("name"), ctrl.get("age"), ctrl.get("birth"), ctrl.get("address"), ctrl.get("phone"), ctrl.get("pym"), ctrl.get("info_id"));

    }

    public String saveInfo(Map<String, String> ctrl) {
        System.out.println(ctrl);
        // ctrl.remove("info_id");
        // ctrl.remove("hospital")
        try {
            ctrl.put("pym", PinYinUtil.getPinYin(ctrl.get("name")));
            if (ctrl.get("age") == null && ctrl.get("birth") != null) {
                ctrl.put("age", "" + DateUtil.getCurrentAgeByBirthdate(ctrl.get("birth")));
            }
            System.out.println(ctrl);
            String hostId = ctrl.get("host_id");
            hostId = hostId == null ? MainTool.getInstance().getHostId() : ctrl.get("host_id");
            for (String key : ctrl.keySet()) {
                if (!key.equals("uuid") && !key.equals("update_id")) {
                    set(key, ctrl.get(key));
                }
            }
            set("host_id", hostId).save();
            return hostId;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public String deletePatient(String pk) {
        try {
            deleteById(pk);
            return "true";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "false";
    }

    public HostInfo getPatientInfo(String host_id) {
        return findFirst("select * from hostinfo where host_id='" + host_id + "'");
    }
    
     public HostInfo getPatientInfo(String info_id,String hospital_code) {
        return findFirst("select * from hostinfo where info_id='" + info_id + "' and hospital_code='"+hospital_code+"'");
    }

}
